<script setup lang="ts">
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import router from "@/router";
import { reactive, ref } from "vue";
import { addParking, getParkingList, getResidentialList, recoverParkingById } from "@/api/property";
import { ElNotification } from "element-plus";


defineOptions({
  name: "parkingList"
});
const form = reactive({
  name: ""
});
const loading = ref(true);
const rentDialog = ref(false);
const formRef = ref();
const dataList = ref([]);
const residentialList = ref([]);
const residential = ref(null);
const rentData = reactive({
  name: "",
  scope: "",
  userId: "",
  id: ""
});

//初始化数据
async function onSearch() {
  //获取小区列表
  getResidentialList().then(res => {
    residentialList.value = res.data.records;
  });
  getShops();
  loading.value = false;
}

//获取商铺
async function getShops() {
  const data = residential;
  console.log(residential.value);
  getParkingList(data.value).then(res => {
    dataList.value = res.data;
  });
}

const rent = (data?: any) => {
  addParking({ id: rentData.id, userId: rentData.userId, status: rentData.scope }).then(res => {
    ElNotification({
      title: "Success",
      message: "车位出租成功",
      type: "success"
    });
  }).catch(res => {
    ElNotification({
      title: "Error",
      message: "车位出租失败",
      type: "error"
    });
  });
  onSearch();
};
const recoverParking = (data?: any) => {
  recoverParkingById(data).then(res => {
    ElNotification({
      title: "Success",
      message: "车位回购成功",
      type: "success"
    });
  }).catch(res => {
    ElNotification({
      title: "Error",
      message: "车位回购失败",
      type: "error"
    });
  });
  onSearch();
};
onSearch();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="车位地址：" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入车位地址"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
        <el-button
          type="success"
          @click="router.push({ name: 'parkingEdit' })"
        >添加车位
        </el-button>
      </el-form-item>
      <br />
      <el-form-item label="归属小区">
        <el-radio-group
          v-model="residential"
          size="small"
          @change="getShops"
        >
          <el-radio-button label="null" value="null">全部</el-radio-button>
          <el-radio-button
            v-for="list in residentialList"
            :label="list.id"
            :value="list.id"
          >{{ list.name }}
          </el-radio-button
          >
        </el-radio-group>
      </el-form-item>
    </el-form>
    <el-table :data="dataList.records" table-layout="auto">
      <el-table-column prop="id" label="ID" />
      <el-table-column label="车位状态" >
        <template #default="scope">
          <el-tag v-if="scope.row.status===0" type="primary">未出租</el-tag>
          <el-tag v-if="scope.row.status===1" type="success">已出租</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="车位地址" />
      <el-table-column prop="userId" label="车位所有人" />
      <el-table-column prop="updated" label="更新时间" />
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            type="primary"
            size="small"
            v-if="scope.row.status===0"
            @click="rentDialog = true;rentData.id=scope.row.id"
          >出租
          </el-button>
          <el-button
            type="danger"
            size="small"
            v-if="scope.row.status===1"
            @click="recoverParking(scope.row.id)"
          >回购
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 100%">
      <el-pagination
        style="float: right"
        background
        layout="prev, pager, next"
        :total="dataList.total"
        :page-count="dataList.current"
      />
    </div>
    <div>
      <!--  使用Dialog弹窗进行出租-->
      <el-dialog v-model="rentDialog" title="车位出租" width="500" center>
    <span>
      用户ID：<el-input v-model="rentData.userId"></el-input>
    </span>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="rentDialog = false">取消</el-button>
            <el-button type="primary" @click="rentData.scope = '1';rent();rentDialog = false">
              确定
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>

</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}</style>
